﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using System.Data;
using System.IO;
using RenShiPinYongBiao.hrdbDataSetTableAdapters;
namespace RenShiPinYongBiao
{
    class TablePrinter
    {
        private PrintPageEventArgs ea;
        private Brush brush = Brushes.Black;
        private Pen pen = Pens.Black;
        private Font fontTitle = new Font("宋体", 30.0f,GraphicsUnit.Pixel);
        private Font fontText = new Font("宋体", 20.0f,GraphicsUnit.Pixel);
        private DataTable dt;
        private StringFormat sfCenter;
        private StringFormat sfLeft;
        private int startX = 45;
        private int startY = 60;
        private int cellW = 80; 
        private int cellH = 75;
        private int index = 0;
        
        public TablePrinter(DataTable d)
        {
            this.dt = d;
            this.sfCenter = new StringFormat();
            sfCenter.Alignment = StringAlignment.Center;
            sfCenter.LineAlignment = StringAlignment.Center;
            this.sfLeft = new StringFormat();
            sfLeft.Alignment = StringAlignment.Near;
            sfLeft.LineAlignment = StringAlignment.Near;
        }
        public void Print(PrintPageEventArgs e){
            this.ea = e;
            this.PrintPage();
            index++;
            if (index >= dt.Rows.Count)
            {
                ea.HasMorePages = false;
            }
            else
            {
                ea.HasMorePages = true;
            }
        }
        private void PrintPage()
        {
            DataRow dr = dt.Rows[index];
            Graphics g = ea.Graphics;
            g.DrawString("事业单位聘用人员登记表", fontTitle, brush, 250, startY);
            g.DrawString("序号：", fontText, brush, startX+10, startY+50);
            g.DrawString(dr["FEmployeeID"].ToString(), fontText, brush, startX + 70, startY + 50);
            g.DrawString("增人计划卡：", fontText, brush, startX+500, startY+50);
            Point tStart = new Point(startX, startY + 90);
            if (!dr["FPhoto"].Equals(System.DBNull.Value))
            {
                Image photo = Image.FromStream(new MemoryStream((Byte[])dr["FPhoto"]));
                g.DrawImage(photo, new Rectangle(startX+cellW*8, tStart.Y, 100, 150));
            }
            g.DrawRectangle(pen, startX + cellW * 8, tStart.Y, 100, 150);
           
            
            
            List<Rectangle> rects = new List<Rectangle>();
            List<String> texts = null;
            for (int j = 0; j <2; j++)
            {
                for (int i = 0; i < 8; i++)
                {
                    Rectangle r = new Rectangle(tStart.X + cellW*i, tStart.Y + cellH * j, cellW, cellH);
                    //g.DrawRectangle(pen,r);
                    rects.Add(r);
                }
            }
            int ytemp = tStart.Y+2*cellH;
            rects.Add(new Rectangle(tStart.X, ytemp, 160, 80));//毕业院校
            rects.Add(new Rectangle(tStart.X + 2 * cellW, ytemp, 320, 80));
            rects.Add(new Rectangle(tStart.X + 6 * cellW, ytemp, 80, 80));
            rects.Add(new Rectangle(tStart.X + 7 * cellW, ytemp, 180, 80));
            cellH += 5;
            ytemp += cellH;
            rects.Add(new Rectangle(tStart.X, ytemp, 160, 80));//聘用前工作单位
            rects.Add(new Rectangle(tStart.X + 2 * cellW, ytemp, 580, 80));
            ytemp += cellH;
            rects.Add(new Rectangle(tStart.X, ytemp, 160, 80));//拟聘岗位
            rects.Add(new Rectangle(tStart.X+2*cellW, ytemp, 320, 80));
            rects.Add(new Rectangle(tStart.X+6*cellW, ytemp, 80, 80));
            rects.Add(new Rectangle(tStart.X+7*cellW, ytemp, 180, 80));
            ytemp += cellH;
            rects.Add(new Rectangle(tStart.X, ytemp, 80, 240));
            rects.Add(new Rectangle(tStart.X+cellW, ytemp, 660, 240));
            ytemp += 240;
            rects.Add(new Rectangle(tStart.X, ytemp, 80, 320));
            rects.Add(new Rectangle(tStart.X+cellW, ytemp, 660, 320));
            DataSourcePinYong source = new DataSourcePinYong(this.dt);
            texts=source.PrepareText(index);

            for(int k=0;k<rects.Count;k++){
                g.DrawString(texts[k],fontText,brush,rects[k],sfCenter);
                g.DrawRectangle(pen,rects[k]);
            }
            cellH -= 5;
        }
    }
}
